﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using TemplateWeb.DTO;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectPhienDauGia()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_PHIENDAUGIA
			};
			return View(CreateViewModel(data));
		}

        public ActionResult CreatePhienDauGia(int idsanpham)
        {
            var data = new DataTransferViewModel
            {
                IDSanPham = idsanpham,
                IntID = idsanpham,
                EnumViewModelType = EnumViewModel.ADMIN_CREATEPHIENDAUGIA
            };
            return View(CreateViewModel(data));
        }

		public ActionResult SelectPhienDauGiaByIDHinhThucDauGia(int idhinhthucdaugia)
		{
			var data = new DataTransferViewModel
			{
				IDHinhThucDauGia = idhinhthucdaugia,
				EnumViewModelType = EnumViewModel.ADMIN_PHIENDAUGIA,
			};
			return View("SelectPhienDauGia", CreateViewModel(data));
		}

		public ActionResult SelectPhienDauGiaByIDHinhThucDauGiaPaging(int idhinhthucdaugia)
		{
			var data = new DataTransferViewModel
			{
				IDHinhThucDauGia = idhinhthucdaugia,
				EnumViewModelType = EnumViewModel.ADMIN_PHIENDAUGIA,
			};
			return PartialView("Templates/TH_ListPhienDauGia", CreateViewModel(data));
		}

		public ActionResult SelectPhienDauGiaByIDDanhMucCon(int iddanhmuccon)
		{
			var data = new DataTransferViewModel
			{
				IDDanhMucCon = iddanhmuccon,
				EnumViewModelType = EnumViewModel.ADMIN_PHIENDAUGIA,
			};
			return View("SelectPhienDauGia", CreateViewModel(data));
		}

		public ActionResult SelectPhienDauGiaByIDDanhMucConPaging(int iddanhmuccon)
		{
			var data = new DataTransferViewModel
			{
				IDDanhMucCon = iddanhmuccon,
				EnumViewModelType = EnumViewModel.ADMIN_PHIENDAUGIA,
			};
			return PartialView("Templates/TH_ListPhienDauGia", CreateViewModel(data));
		}

		public ActionResult SelectPhienDauGiaByIDNguoiDang(Guid idnguoidang)
		{
			var data = new DataTransferViewModel
			{
				IDNguoiDang = idnguoidang,
				EnumViewModelType = EnumViewModel.ADMIN_PHIENDAUGIA,
			};
			return View("SelectPhienDauGia", CreateViewModel(data));
		}

		public ActionResult SelectPhienDauGiaByIDNguoiDangPaging(Guid idnguoidang)
		{
			var data = new DataTransferViewModel
			{
				IDNguoiDang = idnguoidang,
				EnumViewModelType = EnumViewModel.ADMIN_PHIENDAUGIA,
			};
			return PartialView("Templates/TH_ListPhienDauGia", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeletePhienDauGia(int id)
		{
			var deleteItem = _repPhienDauGia.SelectByID(id);
			var result = _repPhienDauGia.Delete(id);
			return Json(new{
				Success = result,
				RecordCount = _repPhienDauGia.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfPhienDauGia(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_PHIENDAUGIA,
				IDPhienDauGia = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdatePhienDauGia(FormCollection forms)
		{
			string id = forms.GetValues("PhienDauGia_ID")[0];
			string ketqua = forms.GetValues("PhienDauGia_KetQua")[0];
            string xuathienotrangchu = forms.GetValues("PhienDauGia_XuatHienOTrangChu")[0];
            string duyet = forms.GetValues("PhienDauGia_Duyet")[0];

            var editItem = _repPhienDauGia.SelectByID(int.Parse(id));
			editItem.KetQua = ketqua;
            editItem.XuatHienOTrangChu = bool.Parse(xuathienotrangchu);
            editItem.Duyet = bool.Parse(duyet);

			var result = false;
			result = _repPhienDauGia.Update(editItem);

			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_PHIENDAUGIA,
				IDPhienDauGia = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfPhienDauGia", CreateViewModel(data));
		}

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertPhienDauGia(FormCollection forms)
        {
            string idhinhthucdaugia = forms.GetValues("hinhthucdaugia")[0];
            string idnguoidang = forms.GetValues("idnguoidang")[0];
            string idsanpham = forms.GetValues("idsanpham")[0]; 

            string thoigianbatdau = forms.GetValues("thoigianbatdau")[0];
            thoigianbatdau += " " + forms.GetValues("giobatdau")[0] + ":" + forms.GetValues("phutbatdau")[0] + ":00";

            string thoigianketthuc = forms.GetValues("thoigianketthuc")[0];
            thoigianketthuc += " " + forms.GetValues("gioketthuc")[0] + ":" + forms.GetValues("phutketthuc")[0] + ":00";

            var newItem = new PhienDauGiaDTO
            {
                IDHinhThucDauGia = int.Parse(idhinhthucdaugia),
                IDSanPham = int.Parse(idsanpham),
                IDNguoiDang = Guid.Parse(idnguoidang),
                ThoiGianBatDau = thoigianbatdau == "" ? DateTime.Now : DateTime.Parse(thoigianbatdau),
                ThoiGianKetThuc = thoigianketthuc == "" ? DateTime.Now : DateTime.Parse(thoigianketthuc),
                KetQua = "Chờ đấu giá",
                XuatHienOTrangChu = true,
                Duyet = true
            };

            if (_repHinhThucDauGia.SelectByID(newItem.IDHinhThucDauGia).TenHinhThucDauGia.ToUpper().Contains("ANH"))
            {
                newItem.GiaKhoiDiem = long.Parse(forms.GetValues("giakhoidiem")[0].Replace(".", string.Empty).Replace(",", string.Empty));
            }
            else
            {
                newItem.GiaToiThieu = long.Parse(forms.GetValues("giatoithieu")[0].Replace(".", string.Empty).Replace(",", string.Empty));
                newItem.TienDatCoc = long.Parse(forms.GetValues("tiendatcoc")[0].Replace(".", string.Empty).Replace(",", string.Empty));
            }

            var result = _repPhienDauGia.Insert(newItem);
            string refName = string.Empty;

            if (result)
            {
                var sanpham = _repSanPham.SelectByID(int.Parse(idsanpham));
                var data = new DataTransferViewModel
                {
                    IDDanhMucCon = sanpham.IDDanhMucCon,
                    EnumViewModelType = EnumViewModel.ADMIN_CREATEPHIENDAUGIA_SUCCESSFUL,
                };
                ViewData["infotext"] = "Tạo phiên mới thành công";
                return View("SelectPhienDauGia", CreateViewModel(data));
            }
            else
            {
                var data = new DataTransferViewModel
                {
                    IntID = int.Parse(idsanpham),
                    EnumViewModelType = EnumViewModel.ADMIN_CREATEPHIENDAUGIA,
                };
                ViewData["infotext"] = "Không thể tạo phiên mới";
                return View("CreatePhienDauGia", CreateViewModel(data));
            }
        }
	}
}